package org.richin.net.http.util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * <p>
 * Title: HttpClientUtil.java
 * </p>
 * <p>
 * Description: 封装HttpClient类
 * </p>
 * 
 * @author gao_jun
 * @version 1.0
 * @created 2009-5-27
 */
public class HttpClientUtil {
	// 存放Post参数
	static List<NameValuePair> nvps = null;
	// 客户端
	public static DefaultHttpClient httpclient = new DefaultHttpClient();

	/**
	 * 通过Post方法取得HTML网页
	 * @param url http地址
	 * @return 取得的Html源码
	 * @throws ClientProtocolException 抛出的异常
	 * @throws IOException 抛出的异常
	 */
	public static String getHtmlByPost(String url) throws ClientProtocolException, IOException {
		String resultStr = "";
		// 提交表单
		HttpPost httpost = new HttpPost(url);
		httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		HttpResponse response = httpclient.execute(httpost);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
		resultStr = EntityUtils.toString(response.getEntity(),"UTF-8" );
		}
		return resultStr;
	}
	private static String getEncode(HttpResponse response){
		Header encodeHeader = response.getEntity().getContentType();
		if (encodeHeader != null) {
			
			String a = encodeHeader.getValue();
			if (a != null) {
				String[] b = a.split(";");
				if (b.length == 2) {
					String[] c = b[1].split("=");
					if (c.length == 2) {
						return c[1];
					}
				}
			}

		}
		return null;
	}
	/**
	 * 通过Get方法来获取Html
	 * @param url http地址
	 * @return 取得的Html源码
	 * @throws ClientProtocolException 抛出的异常
	 * @throws IOException 抛出的异常
	 */
	public static String getHtmlByGet(String url) throws ClientProtocolException, IOException{
		String resultStr = "";
		HttpGet httpget = new HttpGet(url);

		HttpResponse response = httpclient.execute(httpget);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			resultStr = EntityUtils.toString(response.getEntity(), "UTF-8");
		}
		return resultStr;
	}
	
	/**
	 * 设置代理
	 * @param serverIp 代理服务器ip
	 * @param protocol 端口
	 * @param type 协议类型
	 */
	public static void setProxy(String serverIp,String port,String protocol){
		  final HttpHost proxy = new HttpHost(serverIp, new Integer(port).intValue(), protocol);
		  httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
		  proxy);
	}

	/**
	 * 设置参数
	 * 
	 * @param name
	 *            参数名称
	 * @param value
	 *            参数值
	 */
	public static void setPara(String name, String value) {
		nvps.add(new BasicNameValuePair(name, value));
	}

	/**
	 * 创建一个新的列表
	 */
	public static void getNewParas() {
		nvps = new ArrayList<NameValuePair>();
	}
	public static void main(String[] args) throws ClientProtocolException, IOException {
		String response=HttpClientUtil.getHtmlByGet("http://cppupu.javaeye.com/blog/405444");
	System.out.println(response);
	}
}
